package com.gree.auth.config.configuration;

import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 完成连接 及LdapTemplate生成，Ldap统一认证数据查询
 */
public class LdapConfiguration {

    private String url;
    private String usid;
    private String pawd;

    private static final String UID_PREFIX = "it2004\\";
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        Map<String, Object> config = new HashMap();

        contextSource.setUrl(url);
        contextSource.setUserDn((new StringBuilder(UID_PREFIX)).append(usid).toString());
        contextSource.setPassword(pawd);
        //  解决 乱码 的关键一句
        config.put("java.naming.ldap.attributes.binary", "objectGUID");
        contextSource.setPooled(true);
        contextSource.setBaseEnvironmentProperties(config);
        contextSource.afterPropertiesSet();
        return contextSource;
    }

    public LdapTemplate ldapTemplate() {
        LdapTemplate ldapTemplate = new LdapTemplate(contextSource());
        return ldapTemplate;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setUsid(String usid) {
        this.usid = usid;
    }

    public void setPawd(String pawd) {
        this.pawd = pawd;
    }

}